home *** CD-ROM | disk | FTP | other *** search
/ CDUTIL 13 / CDUTIL #13 Julio 1995.iso / windows / acadcom / acrx / sample / gpalsym.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  11.5 KB  |  362 lines

  1. /* Next available MSG number is  33 */
  2.  
  3. /***************************************************************************
  4.    Module Name:  gpalsym.cc
  5.  
  6.    Copyright (C) 1994 by Autodesk, Inc.
  7.  
  8.    Permission to use, copy, modify, and distribute this software in 
  9.    object code form for any purpose and without fee is hereby granted, 
  10.    provided that the above copyright notice appears in all copies and 
  11.    that both that copyright notice and the limited warranty and 
  12.    restricted rights notice below appear in all supporting 
  13.    documentation.
  14.  
  15.    AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.  
  16.    AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF 
  17.    MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC.
  18.    DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE 
  19.    UNINTERRUPTED OR ERROR FREE.
  20.  
  21.    Use, duplication, or disclosure by the U.S. Government is subject to 
  22.    restrictions set forth in FAR 52.227-19 (Commercial Computer 
  23.    Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) 
  24.    (Rights in Technical Data and Computer Software), as applicable.
  25.     
  26.    .
  27.  
  28.    Description:  RXADS sample program which exercises the ads_getsym and
  29.                  ads_putsym functions.
  30.  
  31.    Author     :
  32.                  Autodesk, Inc.
  33.                  2320 Marinship Way
  34.                  Sausalito, CA. 94965
  35.                  (415)332-2344
  36.  
  37.     This Arx application is a conversion from the original sample ADS app
  38.     appmngr.c.
  39.  
  40.     CREATED BY:  William Howison, January 1994
  41.  
  42.     What it tests :
  43.  
  44.     - Setting an AutoLISP variable to a value using ads_putsym.
  45.  
  46.     - Getting an AutoLISP variable using ads_getsym.
  47.  
  48.     Function Entry Points:
  49.       AcRx::AppRetCode
  50.         acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt);
  51.  
  52.     Exported ADS Functions
  53.         TESTGPSYM                 [Test the ADS_XXXSYM functions]
  54.  
  55.     Modification History:
  56.         Jan 27 1994 - bch - original creation
  57.  
  58.     Notes and restrictions on use:
  59.  
  60.  
  61. ***************************************************************************/
  62.  
  63. /**************************************************************************/
  64. /*  MODULE NAME  */
  65. /**************************************************************************/
  66. #define    GPALSYM
  67.  
  68. /****************************************************************************/
  69. /*  DEFINES  */
  70. /****************************************************************************/
  71. #define ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
  72. #define SPOINT(p, x, y, z) p[X] = (x); p[Y] = (y); p[Z] = (z)
  73. #define CPOINT(dst, src) dst[X] = src[X]; dst[Y] = src[Y]; dst[Z] = src[Z]
  74.  
  75. /**************************************************************************/
  76. /*  TYPEDEFS  */
  77. /**************************************************************************/
  78. /* ADS Function Table */
  79. typedef struct {
  80.     char    *name;
  81.     int     (*fptr)();
  82. } ftblent;
  83.  
  84. typedef struct resbuf rbtype;
  85.  
  86. /**************************************************************************/
  87. /*  INCLUDES  */
  88. /**************************************************************************/
  89.  
  90. #include <math.h>
  91. #include <stdio.h>
  92. #include "adslib.h"
  93. #include "rxdefs.h"
  94. #include "ol_errno.h"
  95. #include "adesk.h"
  96.  
  97.  
  98. extern "C" {
  99. /****************************************************************************/
  100. /*  LOCALLY DEFINED ENTRY POINT INVOKED BY Arx                              */
  101. /****************************************************************************/
  102. AcRx::AppRetCode acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt);
  103. }
  104.  
  105. /****************************************************************************/
  106. /*  LOCAL FUNCTION FORWARD DECLARATIONS  */
  107. /****************************************************************************/
  108. int     getsym();
  109. int     putsym();
  110.  
  111. /**************************************************************************/
  112. /*  GLOBAL VARIABLES  */
  113. /**************************************************************************/
  114. /* Table of ADS functions */
  115. ftblent exfun[] = {
  116.             {/*MSG0*/"GETSYM", getsym},
  117.             {/*MSG0*/"PUTSYM", putsym},
  118.         };
  119.  
  120. /**************************************************************************/
  121. /*  EXTERNAL FUNCTION DECLARATIONS  */
  122. /**************************************************************************/
  123.  
  124. /**************************************************************************/
  125. /*  EXTERNAL VARIABLE DECLARATIONS  */
  126. /**************************************************************************/
  127.  
  128. /****************************************************************************/
  129. /*  LOCAL FUNCTION DECLARATIONS  */
  130. /****************************************************************************/
  131. int geterrno   _((void));
  132. int funcload   _((void));
  133. int funcunload _((void));
  134. int dofun      _((void));
  135.  
  136. /******************************************************************************/
  137. /*.doc geterrno(internal) */
  138. /*+
  139.     This function is called to obtain the value of the AutoCAD system
  140.     variable ERRNO and return it as a result.
  141. -*/
  142. /******************************************************************************/
  143. int
  144. /*FCN*/geterrno()
  145. {
  146.     rbtype errval;
  147.  
  148.     ads_getvar(/*MSG0*/"ERRNO", &errval);
  149.  
  150.     return errval.resval.rint;
  151. }
  152.  
  153. /******************************************************************************/
  154. /*.doc funcload(internal) */
  155. /*+
  156.     This function is called to define all function names in the ADS
  157.     function table.  Each named function will be callable from lisp or
  158.     invokable from another ADS application.
  159. -*/
  160. /******************************************************************************/
  161. int
  162. /*FCN*/funcload()
  163. {
  164.     int i;
  165.     for (i = 0; i < ELEMENTS(exfun); i++) {
  166.         if (ads_defun(exfun[i].name, i) != RTNORM)
  167.             return RTERROR;
  168.     }
  169.  
  170.     return RTNORM;
  171. }
  172.  
  173. /******************************************************************************/
  174. /*.doc funclunoad(internal) */
  175. /*+
  176.     This function is called to undefine all function names in the ADS
  177.     function table.  Each named function will be removed from the
  178.     AutoLISP hash table.
  179. -*/
  180. /******************************************************************************/
  181. int
  182. /*FCN*/funcunload()
  183. {
  184.     int i;
  185.  
  186.     /* Undefine each function we defined */
  187.  
  188.     for (i = 0; i < ELEMENTS(exfun); i++) {
  189.         ads_undef(exfun[i].name,i);
  190.     }
  191.  
  192.     return RTNORM;
  193. }
  194. /******************************************************************************/
  195. /*.doc dofun(internal) */
  196. /*+
  197.     This function is called to invoke the function which has the
  198.     registerd function code that is obtained from  ads_getfuncode.  The
  199.     function will return RTERROR if the function code is invalid, or
  200.     RSERR if the invoked function fails to return RTNORM.  The value
  201.     RSRSLT will be returned if the function code is valid and the
  202.     invoked subroutine returns RTNORM.
  203. -*/
  204. /******************************************************************************/
  205. int
  206. /*FCN*/dofun()
  207. {
  208.     int    val;
  209.     int    rc;
  210.  
  211.     ads_retvoid();
  212.  
  213.     if ((val = ads_getfuncode()) < 0 || val > ELEMENTS(exfun))
  214.         return RTERROR;
  215.  
  216.     rc = (*exfun[val].fptr)();
  217.  
  218.     return ((rc == RTNORM) ? RSRSLT:RSERR);
  219. }
  220.  
  221. /******************************************************************************/
  222. /*.doc getsym(internal) */
  223. /*+
  224.     This function is called from dofun function as a result of RQSUBR
  225.     request being sent to the main dispatch loop.  It requires one
  226.     string argument which specifies the name of an AutoLISP symbol
  227.     whos value should be returned.
  228.  
  229.     This function always returns RTNORM.  An error message will be
  230.     displayed in the event of an error, and nil will be returned.
  231.     Otherwise, the value of the AutoLISP symbol will be returned
  232.     to the AutoLISP caller.
  233. -*/
  234. /******************************************************************************/
  235. int
  236. /*FCN*/getsym()
  237. {
  238.     rbtype      *args;
  239.     int         rc;
  240.     rbtype      *varval = NULL;
  241.  
  242.     args = ads_getargs();
  243.     if (args == NULL || args->restype != RTSTR) {
  244.         ads_printf(/*MSG3*/"Requires an AutoLISP symbol name in \"s.\n");
  245.         return RTNORM;
  246.     }
  247.  
  248.     rc = ads_getsym(args->resval.rstring, &varval);
  249.     if (rc != RTNORM) {
  250.         switch(geterrno()) {
  251.         case OL_ESYMNAM:
  252.             ads_printf(/*MSG4*/"Invalid AutoLISP symbol name.\n");
  253.             break;
  254.         case OL_ENULLPTR:
  255.             ads_printf(/*MSG5*/"Passed NULL pointer to ads_getsym.\n");
  256.             break;
  257.         case OL_ENEWRB:
  258.             ads_printf(/*MSG6*/"Can't allocate result buffer.\n");
  259.             break;
  260.         case OL_ESYMVAL:
  261.             ads_printf(/*MSG7*/"Invalid symbol value.\n");
  262.             break;
  263.         }
  264.     } else {
  265.        if (varval != NULL) {
  266.            if (varval->rbnext == NULL)
  267.                ads_retval(varval);
  268.            else
  269.                ads_retlist(varval);
  270.        }
  271.     }
  272.     if (varval != NULL)
  273.         ads_relrb(varval);
  274.  
  275.     return RTNORM;
  276. }
  277.  
  278. /******************************************************************************/
  279. /*.doc putsym(internal) */
  280. /*+
  281.     This function is called from dofun function as a result of RQSUBR
  282.     request being sent to the main dispatch loop.  It requires two
  283.     arguments.  The first argument is a string containing the name of
  284.     an AutoLISP symbol to set.  The second argument can be any data
  285.     type or list containing any data type which can be represented
  286.     in an ADS resbuf structure.
  287.  
  288.     This function always returns RTNORM to the C caller.  This function
  289.     always return nil if the operation fails.  Otherwise, the value of
  290.     the second argument is returned.
  291.  
  292.     A message will be displayed if an error occurs, and the value
  293.     returned to the AutoLISP caller will be nil.
  294. -*/
  295. /******************************************************************************/
  296. int
  297. /*FCN*/putsym()
  298. {
  299.     rbtype      *args;
  300.     int         rc;
  301.  
  302.     args = ads_getargs();
  303.     ads_retnil();
  304.  
  305.     if (args == NULL || args->restype != RTSTR) {
  306.         ads_printf(/*MSG8*/"Requires name of AutoLISP symbol in \"s, and\n");
  307.         ads_printf(/*MSG9*/"value to be assigned to symbol.\n");
  308.     }
  309.     
  310.     rc = ads_putsym(args->resval.rstring, args->rbnext);
  311.     if (rc != RTNORM) {
  312.         switch(geterrno()) {
  313.         case OL_ESYMNAM:
  314.             ads_printf(/*MSG10*/"Invalid AutoLISP symbol name.\n");
  315.             break;
  316.         case OL_ESYMVAL:
  317.             ads_printf(/*MSG11*/"Invalid symbol value.\n");
  318.             break;
  319.         }
  320.     } else {
  321.         if (args->rbnext != NULL) {
  322.             if (args->rbnext->rbnext == NULL)
  323.                 ads_retval(args->rbnext);
  324.             else
  325.                 ads_retlist(args->rbnext);
  326.         }
  327.     }
  328.     return RTNORM;
  329. }
  330.  
  331. /* =================== Arx Module Interface Functions ================ */
  332.  
  333. AcRx::AppRetCode
  334. /*FCN*/acrxEntryPoint(AcRx::AppMsgCode msg, void * ptr)
  335. {
  336.  
  337.     if (ptr != NULL) {
  338.         // We have been handed some kind of object
  339.         // but we aren't going to do anything with it.
  340.     }
  341.  
  342.     switch(msg) {
  343.         case AcRx::kInitAppMsg:
  344.             break;
  345.         case AcRx::kInvkSubrMsg:
  346.             dofun();
  347.             break;
  348.         case AcRx::kLoadADSMsg:
  349.             funcload();
  350.             break;
  351.         case AcRx::kUnloadADSMsg:
  352.             funcunload();
  353.             ads_printf(/*MSG2*/"Unloading.\n");
  354.             break;
  355.         case AcRx::kUnloadAppMsg:
  356.         default:
  357.             break;
  358.     }
  359.     return AcRx::kRetOK;
  360. }
  361.  
  362.